<?php
namespace app\admin\controller;
// namespace app\index\module;
use app\common\controller\Adminbase;
use think\Controller;
use think\Db;
use think\Request;


import('Curl', EXTEND_PATH);
import('Interfaces', EXTEND_PATH);

//class Order extends Adminbase{
  class Order extends controller {

    //订单管理（玩家订单）
    public function order(){
        return $this->fetch();
    }
    //订单管理（会长订单）
    public function order_1(){
        return $this->fetch();
    }
    //玩家订单查询
    public function player_serach(){
        $id = input("param.id");  //订单号
        $player_id = input("param.player_id");//玩家ID
        $product_id = input("param.product_id");//商品ID
        $start_time = input("param.start_time");//时间
        $end_time = input("param.end_time");//时间
        $order_status = input("param.order_status");
        $where= [];
        $where['type'] = 0;
        if($id != 0){
            $where['id'] =$id;
        }
        if($order_status != ""){
            $where['order_status'] =$order_status;
        }
        if($player_id !=0){
            $where['player_id'] =$player_id;
        }
        if($product_id !=""){
            $where['product_id'] =$product_id;
        }
        if($start_time !="" && $end_time !=""){
            $where['order_time'] =array(array('Egt',$start_time),array('Elt',$end_time), 'and');
        }
        if($id==""&&$player_id==""&&$product_id==""&& $start_time==""&&$end_time==""&&$order_status==""){
            $starTime = date('Y-m-d', strtotime('-2 days'))." "."00:00:00";
            $endTime = date('Y-m-d', time())." "."23:59:59";
            $where['order_time'] =array(array('Egt',$starTime),array('Elt',$endTime), 'and');
            $where['order_status'] =1;

        }
        $playdata = Db::name('order')
            ->alias('o')
            ->where($where)
            ->order('o.order_time desc')
            ->select();
        foreach ($playdata as $key => $value) {
            $cid = $value['club_id'];
            $result = Db::name('club')
                ->where("cid",$cid)
                ->find();
            $playdata[$key]['club_name'] = base64_decode($result['club_name']);

            // $url = USER_HTTP."api/get_info.php";
            // $data=[
            //     'app_id'=>APP_ID,
            //     'uid'=>$value['player_id']
            // ];
            // $data = json_encode($data);

            // $curl = new \Curl();
            // $result = $curl->vpost($url,$data);
			  $playdata[$key]['player_name'] = base64_decode($value['player_name']);
            // $result = postInterface($url,$data);
            // $result_obj =  json_decode($result);
            // print_r($result_obj);
            // $playdata[$key]['nickname'] =@$result_obj->data->nickname;
            // $playdata[$key]['nickname'] =$result_obj->data->nickname;
        }
        return json($playdata);
    }

    //会长订单查询
    public function president_search(){
        $id = input("param.id");  //订单号
        $player_id = input("param.player_id");//玩家ID
        $product_id = input("param.product_id");//商品ID
        $start_time = input("param.start_time");//时间
        $end_time = input("param.end_time");//时间
        $order_status = input("param.order_status");
        $where= [];
        $where['type'] = 1;
        if($id != 0){
            $where['id'] =$id;
        }
        if($player_id !=0){
            $where['player_id'] =$player_id;
        }
        if($product_id !=""){
            $where['product_id'] =$product_id;
        }
        if($start_time !="" && $end_time !=""){
            $where['order_time'] =array(array('Egt',$start_time),array('Elt',$end_time), 'and');
        }
        if($order_status != ""){
            $where['order_status'] =$order_status;
        }
        if($id==""&&$player_id==""&&$product_id==""&& $start_time==""&&$end_time==""&&$order_status==""){
            $starTime = date('Y-m-d', strtotime('-30 days'))." "."00:00:00";
            $endTime = date('Y-m-d', time())." "."23:59:59";
            $where['order_time'] =array(array('Egt',$starTime),array('Elt',$endTime), 'and');
            $where['order_status'] =1;

        }
        $presidentdata = Db::name('order')
            ->alias('o')
            ->where($where)
            ->order('o.order_time desc')
            ->select();
        foreach ($presidentdata as $key => $value){
            $cid = $value['club_id'];
            $result = Db::name('club')
                ->where("cid",$cid)
                ->find();
            $presidentdata[$key]['fee'] =$value['fee']/100;
            $presidentdata[$key]['president_name'] = $result['president_name'];
            $presidentdata[$key]['club_name'] = base64_decode($result['club_name']);
        }
        return json($presidentdata);
    }
}
